<?php
// +----------------------------------------------------------------------
// | 预存/还款、充值处理页面，当 did 有值时为预存/还款
// +----------------------------------------------------------------------
// | Author: TongBC <501468940@qq.com>
// +----------------------------------------------------------------------
// | Date：2015/08/11
// +----------------------------------------------------------------------
include_once('/var/www/html/new/com.inc.php');
if(!$_SESSION['sid']) exit("<script>window.location='dlogin.html'</script>");

// 充值数据        Array ( [alipay] => 123456789 [money] => 1000 )
// 预存/还款数据   Array ( [did] => 81 [act] => r [money] => 1 )

function uuid() {
	$uid=$_SESSION['sid'];
	$c=7-strlen($uid);
	$f=$c?rand(pow(10,($c-1)),(pow(10,$c)-1)):"";
	return uniqid($f.$uid);
}

$aid    = $_REQUEST["did"];    // 供应商
$money  = $_REQUEST["money"];  // 预存/还款金额
$actual = $_REQUEST["actual"];
$alipay = $_REQUEST["alipay"];

do{// 构造一个平台没有的商户订单号
	$has = "";
	$out_trade_no = uuid();
	$sel = "select id from pft_alipay_rec where out_trade_no='$out_trade_no' limit 1";
	$GLOBALS['le']->query($sel);
	$GLOBALS['le']->fetch_assoc();
	$has = $GLOBALS['le']->f("id");
}while($has);


// 支付手续费
$did = $aid ? $aid:$_SESSION['sid'];
$sel="select dname,alipay,fee_bank from pft_member where id=$did limit 1";
$GLOBALS['le']->query($sel);
$GLOBALS['le']->fetch_assoc();
$fee_bank = isset($_REQUEST["actual"]) ? $actual-$money:round($money*$GLOBALS['le']->f("fee_bank")/1000,2);
$aname    = $GLOBALS['le']->f("dname");  // 仅aid时
$a_alipay = $GLOBALS['le']->f("alipay"); // 仅aid时


$sel="select dtype,alipay,dname from pft_member where id=".$_SESSION['sid']." limit 1";
$GLOBALS['le']->query($sel);
$GLOBALS['le']->fetch_assoc();
$dname  = $GLOBALS['le']->f("dname");
$dtype  = $GLOBALS['le']->f("dtype");
$alipay = $alipay ? $alipay:$GLOBALS['le']->f("alipay");

switch($dtype){
	case 0:$dt="供应商";break;
	case 1:$dt="分销商";break;
	case 2:$dt="直接供应商";break;
	case 5:$dt="普通用户";break;
}


$a_key[] = 'WIDout_trade_no';
$a_key[] = 'WIDsubject';
$a_key[] = 'WIDtotal_fee';
$a_key[] = 'WIDbody';
$a_key[] = 'WIDshow_url';
$a_key[] = 'WIDbuyer';
$a_key[] = 'notify_url';
$a_key[] = 'return_url';

$a_value[] = $out_trade_no;// 商户订单号,商户网站订单系统中唯一订单号，必填
$a_value[] = $aid?'充值给供应商':$dt.$dname.'充值';// 订单名称或产品名称，必填
$a_value[] = isset($_REQUEST["actual"])?$actual:$money;// 付款金额，必填

if($aid){
	$mask = '系统备注：'.$dname.'['.$_SESSION['sid'].']预存/还款'.$money.'元，供应商：'.$aname.'['.$aid.'];用户备注：';
	$mask.= ($_REQUEST['memo']) ? $_REQUEST['memo']:'无';
	$a_value[] = $mask.'|'.$_SESSION['sid'].'|'.$aid.( $a_alipay ? "":"|1" );
	
}else{
	$a_value[] = '票付通'.$dt.$dname.'充值资金账户'.$money.'元。|'.$_SESSION['sid'];//订单描述
}

$a_value[] = '';// 产品展示路径 例如：http://www.12301.cc/s_01.html
$a_value[] = $alipay;// 买家绑定的支付宝帐号 最好填上
$a_value[] = 'http://www.12301.cc/alipay/ali_recharge/'.( $aid ? 'notify_url_tm.php':'notify_url.php' );
$a_value[] = 'http://www.12301.cc/alipay/ali_recharge/return_url.php';



function make_post($id, $vid){
    $post_data = array();
    if (!is_array($id) && !is_array($vid)){
        $post_data[$id] = $vid;
    }
    else{
        foreach ($id as $kk=>$vv){
            $post_data[$vv]=$vid[$kk];
        }
    }
    $url='http://pay.12301.cc/alipayapi.php';
    $o="";
    foreach ($post_data as $k=>$v){
        $o.= "$k=".urlencode($v)."&";
    }
    $post_data=substr($o,0,-1);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);
    return curl_exec($ch);
}
make_post($a_key,$a_value);
?>